import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeItemPage extends StatefulWidget {
  final num pageIndex;
  final String pageTitle;
  HomeItemPage(this.pageIndex, this.pageTitle);
  @override
  _HomeItemPageState createState() => _HomeItemPageState();
}

class _HomeItemPageState extends State<HomeItemPage> {
  List<String> imageList = [
    "https://cdn.pixabay.com/photo/2017/07/10/23/45/cubes-2492010_960_720.jpg",
    "https://cdn.pixabay.com/photo/2018/06/07/16/49/vr-3460451_960_720.jpg",
    "https://cdn.pixabay.com/photo/2015/11/09/14/43/laptop-1035345_960_720.jpg"
  ];
  int bannerIndex = 0;
  RefreshController refreshController = new RefreshController();
  // 下拉刷新 水滴效果
  Widget refreshWaterDropHeader = WaterDropHeader(waterDropColor: Colors.blue);
  // 这个自定义下拉条 只显示小圈圈
  Widget refreshMaterialClassicHeader =
      MaterialClassicHeader(color: Colors.blue, backgroundColor: Colors.white);
  // 下拉 划出效果
  Widget refreshClassicHeader = ClassicHeader();

  // MaterialClassicHeader(color:Colors.blue,backgroundColor:Colors.white); 这个自定义下拉条 只显示小圈圈
  //  这个headre 有一个出来部分的效果
  // ClassicHeader(
  //     idleText: "下拉刷新",
  //     releaseText: "释放刷新",
  //     refreshingText: "刷新中",
  //     failedText: "刷新失败",
  //     completeText: "刷新成功");

  //  defaultTargetPlatform == TargetPlatform.iOS
  //     ? ClassicHeader(
  //         idleText: "下拉刷新",
  //         releaseText: "释放刷新",
  //         refreshingText: "刷新中",
  //         failedText: "刷新失败",
  //         completeText: "刷新成功")
  //     : WaterDropHeader(
  //         refresh: Text("加载中..."),
  //         complete: Text("刷新成功"),
  //         failed: Text("刷新失败"),
  //       );

  final LoadIndicator defaultFooter = ClassicFooter();

  // 在配置了本地的语言支持的时候，就会根据本地的预览才展示默认英文
  // ClassicFooter(
  //     idleText: "上拉加载",
  //     failedText: "加载失败",
  //     loadingText: "加载中...",
  //     noDataText: "没有更多啦",
  //     canLoadingText: "松开加载");

  // CustomFooter(
  //   builder: (BuildContext context, LoadStatus mode) {
  //     Widget body;
  //     if (mode == LoadStatus.idle) {
  //       body = Text("上拉加载");
  //     } else if (mode == LoadStatus.loading) {
  //       body = CupertinoActivityIndicator();
  //     } else if (mode == LoadStatus.failed) {
  //       body = Text("加载失败！点击重试！");
  //     } else if (mode == LoadStatus.canLoading) {
  //       body = Text("松手,加载更多!");
  //     } else {
  //       body = Text("没有更多数据了!");
  //     }
  //     return Container(
  //       height: 55.0,
  //       child: Center(child: body),
  //     );
  //   },
  // );

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[300],
      child: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: refreshWaterDropHeader,
        footer: defaultFooter,
        controller: refreshController,
        onRefresh: onRefresh,
        onLoading: onLoading,
        physics: BouncingScrollPhysics(),
        child: buildListView(),
      ),
    );
  }

  Widget buildBannerWidget() {
    return Container(
      height: 200,
      child: Stack(
        children: [
          Positioned(
            top: 0,
            bottom: 0,
            left: 0,
            right: 0,
            child: Swiper(
              onIndexChanged: (value) {
                setState(() {
                  bannerIndex = value;
                });
              },
              itemCount: imageList.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 200,
                  child: Stack(
                    children: [
                      Positioned(
                        child:
                            Image.network(imageList[index], fit: BoxFit.fill),
                        left: 0,
                        right: 0,
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
          Positioned(
            bottom: 10,
            right: 10,
            child: Container(
              padding: EdgeInsets.only(
                left: 5,
                right: 5,
                top: 2,
                bottom: 2,
              ),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(10),
                  )),
              child: Text(
                "${bannerIndex + 1}/${imageList.length}",
                style: TextStyle(fontSize: 10),
              ),
            ),
          ),
        ],
      ),
    );
  }

  onRefresh() async {
    print("下拉刷新回调");
    Future.delayed(new Duration(milliseconds: 3000), () {
      refreshController.refreshCompleted();
    });
  }

  onLoading() async {
    print("上拉加载回调");
    Future.delayed(new Duration(milliseconds: 3000), () {
      // refreshController.loadComplete();
      refreshController.loadNoData();
    });
  }

  buildListView() {
    return ListView.builder(
        padding: EdgeInsets.only(top: 0),
        itemCount: 10,
        physics: BouncingScrollPhysics(),
        itemBuilder: (BuildContext context, int index) {
          if (index == 0 && widget.pageIndex == 0) {
            return buildBannerWidget();
          }
          return Container(
            height: 130,
            margin: EdgeInsets.only(top: 5),
            padding: EdgeInsets.all(10),
            color: Colors.white,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("GGB flutter奇遇记",
                    style:
                        TextStyle(fontSize: 17, fontWeight: FontWeight.bold)),
                SizedBox(height: 10),
                Text(
                  "我喜欢并习惯了对变化的东西保持着距离，这样才会知道什么是最不会被时间抛弃的。比如爱一个人，充满了变数，我于是后退一步，精进的看着，知道看到真诚的感情。",
                  overflow: TextOverflow.ellipsis,
                  maxLines: 2,
                  style: TextStyle(color: Colors.grey, fontSize: 14),
                ),
                SizedBox(height: 10),
                Row(
                  children: [
                    Text("GGB-Hacker",
                        style:
                            TextStyle(fontSize: 12, color: Colors.grey[500])),
                    SizedBox(width: 16),
                    Icon(
                      Icons.thumb_up,
                      size: 16,
                      color: Colors.grey[500],
                    ),
                    SizedBox(width: 6),
                    Text("20",
                        style:
                            TextStyle(fontSize: 12, color: Colors.grey[500])),
                    SizedBox(width: 16),
                    Icon(
                      Icons.share,
                      size: 16,
                      color: Colors.grey[500],
                    ),
                    SizedBox(width: 6),
                    Text("200",
                        style:
                            TextStyle(fontSize: 12, color: Colors.grey[500])),
                  ],
                ),
              ],
            ),
          );
        });
  }
}
